package com.xlf.book.dao.impl;

import com.xlf.book.dao.BookInfoDao;
import com.xlf.book.po.BookInfo;
import com.xlf.book.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-07-17-17:16
 * @description 图书信息数据接口实现类
 */
public class BookInfoDaoImpl implements BookInfoDao {
    @Override
    public List<BookInfo> queryAll(String name) {
        List<BookInfo> list = new ArrayList<>();
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from bookinfo where bookName like ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
            rs = ps.executeQuery();
            list = (List<BookInfo>) DButil.infoRs(rs,BookInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return list;
    }

    @Override
    public List<BookInfo> queryAll() {
        List<BookInfo> list = new ArrayList<>();
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from bookinfo";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            list = (List<BookInfo>) DButil.infoRs(rs,BookInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return list;
    }

    @Override
    public boolean deleteInfo(Integer typeId) {
        String sql = "delete from bookinfo where id = ?";
        Object []  param = {typeId};
        return DButil.update(sql,param);
    }

    @Override
    public boolean addInfo(BookInfo info) {
        String sql = "insert into bookinfo values(?,?,?,?,?,?,?)";
        Object [] param = {info.getId(),info.getBookName(),info.getPrice(),info.getTypeId(),info.getBornDate(),info.getImgUrl(),info.getState()};
        return DButil.update(sql,param);
    }

    @Override
    public Integer queryCount() {
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer count = null;
        String sql = "select count(*) from bookinfo";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()){
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return count;
    }

    @Override
    public BookInfo queryById(Integer id) {
        BookInfo info = null;
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from bookInfo where id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                info = new BookInfo();
                info.setId(rs.getInt("id"));
                info.setBookName(rs.getString("bookName"));
                info.setPrice(rs.getDouble("price"));
                info.setTypeId(rs.getInt("typeId"));
                info.setBornDate(rs.getDate("bornDate"));
                info.setImgUrl(rs.getString("imgUrl"));
                info.setState(rs.getInt("state"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return info;
    }

    @Override
    public boolean update(BookInfo info) {
        String sql = "update bookInfo set bookName = ? , price = ? , typeId = ? , bornDate = ? , imgUrl = ? , state = ? where id = ?";
        Object [] param = {info.getBookName(),info.getPrice(),info.getTypeId(),info.getBornDate(),info.getImgUrl(),info.getState(),info.getId()};
        return DButil.update(sql,param);
    }
}
